<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
</head>
<body style="overflow-x:hidden">
<div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-header"><h2>训练数据</h2></div>
            <div class="layui-card-body">
                <div class="layui-upload">
                    <div class="site-button" style="margin-top: 20px; margin-bottom: 0;">
                        <button type="button" class="layui-btn layui-btn-normal" id="imgFileList">选择多文件</button>
                        <div class="layui-inline">
                            <label class="layui-form-label">图像人名:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" id="personName" autocomplete="off" class="layui-input" placeholder="请输入 图像人名" />
                            </div>
                        </div>
                        <button type="button" class="layui-btn site-active" id="listAction" data-type="loading">开始上传
                        </button>
                    </div>
                    <div class="layui-progress layui-progress-big" lay-showpercent="true"
                         lay-filter="uploadProgress"
                         style="margin-top: 20px;">
                        <div class="layui-progress-bar layui-bg-bar" lay-percent="0%"></div>
                    </div>
                    <div class="layui-upload-list">
                        <table class="layui-table">
                            <thead>
                            <tr>
                                <th>文件名</th>
                                <th>预览</th>
                                <th>大小</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="fileList"></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/static/assets/js/jquery.min.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    let prefix = '/admin';
    layui.use(['element', 'upload'], function () {
        let $ = layui.jquery, element = layui.element, upload = layui.upload;
        let activeBtn = $('.site-active');
        let DISABLED = 'layui-btn-disabled';
        let loading;
        //多文件列表上传
        var demoListView = $('#fileList'), uploadListIns = upload.render({
            elem: '#imgFileList',
            url: prefix + '/uploadList', // 上传接口
            accept: 'images',
            acceptMime: 'image/*',
            multiple: true,
            auto: false,
            bindAction: '#listAction',
            loading: true,
            // 选择文件后的回调函数
            choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" style="height: 66px;"></td>'
                        , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });
                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });
                    demoListView.append(tr);
                });
            },
            // 文件提交上传前的回调
            before: function (obj) {
                var name = $('#personName').val();
                loading = layer.load(); //上传loading
                if (obj.aborted <= 0) {
                    layer.close(loading);
                    layer.msg("请选择文件！", {icon: 4});
                    return false; // 需修改upload.js源码，才可以终止上传
                } else if (name === undefined || name === '') {
                    layer.close(loading);
                    layer.msg("请输入图像人名！", {icon: 4});
                    return false; // 需修改upload.js源码，才可以终止上传
                }
                this.data = {'personName': name}; // 传输人名参数
            },
            progress: function (n, elem) {
                element.progress('uploadProgress', n + '%'); // 配合 layui 进度条元素使用，n为百分占比
            },
            done: function (res, index, upload) {
                if (!activeBtn.hasClass(DISABLED)) {
                    activeBtn.addClass(DISABLED);
                }
                if (res.code === 0) { //上传成功
                    var tr = demoListView.find('tr#upload-' + index), tds = tr.children();
                    tds.eq(3).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(4).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            },
            error: function (index, upload) {
                var tr = demoListView.find('tr#upload-' + index), tds = tr.children();
                tds.eq(3).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(4).find('.demo-reload').removeClass('layui-hide'); //显示重传
                layer.close(loading); //关闭loading
            },
            allDone: function (obj) { //当文件全部被提交后，才触发
                layer.close(loading); //关闭loading
                // obj.total 得到总文件数
                // obj.successful 请求成功的文件数
                // obj.aborted 请求失败的文件数
                activeBtn.removeClass(DISABLED);
                if (obj.aborted <= 0) {
                    layer.msg(obj.total + "个文件上传成功！", {icon: 1});
                } else {
                    layer.msg(obj.aborted + "个文件上传失败！", {icon: 2});
                }
            }
        });
    });
</script>
</body>
</html>